<template>
  <div v-loading="loading" class="app-container">
    <div class="search-box">
      <div class="search-form">
        <el-form ref="searchForm" :inline="true" :model="searchForm">
          <el-form-item label="流程名" prop="name">
            <el-input v-model="searchForm.name" maxlength="100" autocomplete="off" />
          </el-form-item>
          <el-form-item label="流程ID" prop="key">
            <el-input v-model="searchForm.key" maxlength="100" autocomplete="off" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
            <el-button icon="el-icon-remove-outline" title="重置" @click="resetSearchForm" />
          </el-form-item>
        </el-form>
      </div>
      <div class="toolbar-box clearfix">
        <el-button type="primary" icon="el-icon-circle-plus" @click="add">新增</el-button>
        <el-button type="info" icon="el-icon-upload2" @click="impProcess">导入流程</el-button>
      </div>
    </div>
    <el-table
      ref="multipleTable"
      stripe
      :data="tableData"
      border
      class="table"
    >
      <el-table-column prop="name" label="流程名称" />
      <el-table-column prop="key" label="流程ID" />
      <el-table-column prop="version" label="版本" />
      <el-table-column prop="deploymentTime" label="部署时间" width="170">
        <template slot-scope="scope">
          <span>{{ scope.row.deploymentTime | parseTime("{y}-{m}-{d} {h}:{i}") }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="suspended" label="状态">
        <template slot-scope="scope">
          <span>{{ scope.row.suspended?'挂起':'激活' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="210" align="left">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" @click="handleDesign(scope.row.id)">设计</el-button>
          <el-button v-if="!scope.row.suspended" type="text" icon="el-icon-video-pause" class="orange" @click="handleSuspend(scope.row)">挂起</el-button>
          <el-button v-else type="text" icon="el-icon-video-play" class="green" @click="handleActive(scope.row)">激活</el-button>
          <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" :page.sync="curPage" :limit.sync="limit" @pagination="getList" />
    <!--表单组件-->
    <eForm ref="eform" />
    <impForm ref="impform" />
  </div>
</template>

<script>
import request from '@/utils/request'
import Pagination from '@/components/Pagination'
import eForm from './form'
import impForm from './impform'
export default {
  name: 'ProcessDef',
  components: { Pagination, eForm, impForm },
  data() {
    return {
      curPage: 1,
      limit: 5,
      total: 0,
      loading: true,
      downloadLoading: false,
      orderByClause: null,
      tableData: [],
      searchForm: {
        name: '',
        key: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取数据
    getList() {
      this.loading = true
      const param = {}
      const queryPage = {}
      queryPage.limit = this.limit
      queryPage.offset = this.limit * (this.curPage - 1)
      param.page = queryPage
      if (this.orderByClause && this.orderByClause.trim() !== '') {
        param.orderByClause = this.orderByClause
      }
      this.copyNotBlankField(this.searchForm, param)
      request({ url: '/sys/process/list', method: 'post', data: param })
        .then(res => {
          this.loading = false
          this.tableData = res.rows
          this.total = res.total
          this.curPage = res.pageNumber
        })
        .catch(e => {
          this.loading = false
          this.$message.error('查询出错')
        })
    },
    onSearch() {
      this.getList()
    },
    resetSearchForm() {
      this.$refs['searchForm'].resetFields()
    },
    add() {
      this.$refs.eform.add()
    },
    impProcess() {
      this.$refs.impform.show()
    },
    handleDesign(id) {
      return this.$router.push({ path: '/sys/processdesign?id=' + id })
    },
    handleDelete(row) {
      this.$confirm('此操作将删除[' + row.name + '], 是否继续?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doDelete(row.deploymentId)
        })
    },
    doDelete(id) {
      this.loading = true
      request({ url: '/sys/process/' + id, method: 'delete' })
        .then(res => {
          this.loading = false
          this.getList()
        })
        .catch(e => {
          this.loading = false
          this.$message.error('删除出错！' + e.message ? e.message : '')
        })
    },
    handleSuspend(row) {
      this.$confirm('此操作将挂起[' + row.name + '], 是否继续?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doHandleSuspend(row)
        })
    },
    doHandleSuspend(row) {
      this.loading = true
      request({ url: '/sys/process/suspendById', method: 'get', params: { 'id': row.id }})
        .then(res => {
          this.loading = false
          this.getList()
        })
        .catch(e => {
          this.loading = false
          this.$message.error('挂起错误！' + e.message ? e.message : '')
        })
    },
    handleActive(row) {
      this.$confirm('此操作将激活[' + row.name + '], 是否继续?', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doHandleActive(row)
        })
    },
    doHandleActive(row) {
      this.loading = true
      request({ url: '/sys/process/activeById', method: 'get', params: { 'id': row.id }})
        .then(res => {
          this.loading = false
          this.getList()
        })
        .catch(e => {
          this.loading = false
          this.$message.error('激活错误！' + e.message ? e.message : '')
        })
    }

  }
}
</script>
